<?php
namespace app\api\controller\album;
use app\common\controller\Api;
use think\Db;
use app\common\model\Attachment;
class Photo extends Api{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected function _initialize()
    {
        parent::_initialize();

    }

    public function del(){
        $userid = $this->auth->id;
        $ids = $this->request->post('ids');
        if(empty($ids)){
            $this->error('参数错误');
        }
        $data = explode(',',$ids);
        if(is_array($data)){
            foreach ($data as $key => $value) {
                Db::name('AlbumPhoto')->where('id',$value)->delete();
            }
        }
        $this->success('相册图片删除成功');
    }
    public function sort(){
        $userid = $this->auth->id;
        $data = $this->request->post('data');
        if($data){
            foreach ($data as $key => $value) {
                Db::name('AlbumPhoto')->where('id',$value['id'])->update(['weigh'=>$value['weigh']]);
            }
        }
        $this->success('相册排序完成');
    }

    public function move(){
        $userid = $this->auth->id;
        $images = $this->request->post('images/a');
        $album_id = $this->request->post('catalog_id');
        $count = Db::name('AlbumPhoto')->where('album_catalog_id',$album_id)->count('id')?:0;
        if(!empty($images)){
            foreach ($images as $key => $value) {
                Db::name('AlbumPhoto')->where('id',$value['id'])->update(['album_catalog_id'=>$album_id,'weigh'=>$count+$value['weigh']]);
            }
            
        }
        $this->success('相册图片移动完成');
    }

    public function info(){
        $userid = $this->auth->id;
        $id = $this->request->param('id');
        $image = Db::name('AlbumPhoto')->where('id',$id)->find();
        
        $info = Db::name('Attachment')->where('url',$image['image'])->find();
        $image['image'] = cdnurl($image['image'])."?x-oss-process=image/resize,w_750";
        $image['createtime'] = date('Y-m-d H:i:s',$image['createtime']);
        if(!empty($info)){
            $image['resolution'] = $info['imagewidth'].'X'.$info['imageheight'];
            $image['size'] = round($info['filesize']/1024,2).'KB';
            $image['user'] = $info['user_id']>0?Db::name('User')->where('id',$info['user_id'])->value('nickname') :'未知';
        }else{
            $image['resolution'] = '未知';
            $image['size'] ='未知';
            $image['user'] = '未知';
        }
        $this->success('相册图片详情',$image);
    }
}